package com.gbits.app.finance.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.springframework.transaction.annotation.Transactional;

import com.gbits.app.finance.api.Page;
import com.gbits.app.finance.dao.IBaseDao;


@Transactional
public abstract class AbstractService<E> implements BaseService<E> {

	public abstract IBaseDao<E> getBaseDao();
	
	public <T> Page<T> queryByPage(final Class<T> entityClass,
			final Criterion[] criterions, final Order[] orders,
			final Page<T> page) {
		return this.getBaseDao().queryByPage(entityClass, criterions, orders, page);
	}

	public E create(E entity) {
		return this.getBaseDao().create(entity);
	}

	public void delete(Serializable id) {
		E e = this.getBaseDao().load(id);
		if (null != e) {
			List<E> list = new ArrayList<E>();
			list.add(e);
			this.getBaseDao().delete(list);
		}
	}

	public void update(E entity) {
		this.getBaseDao().update(entity);
	}

	public E get(Serializable id) {
		return this.getBaseDao().get(id);
	}

	public void saveAll(Collection<E> list) {
		if (null == list || list.isEmpty()) {
			return;
		}
		for (E e : list) {
			this.getBaseDao().create(e);
		}
	}

	public List<E> findAll() {
		return this.getBaseDao().findAll();
	}

	public void deleteAll() {
		List<E> all = this.findAll();
		this.getBaseDao().delete(all);
	}
	
	
	
}
